package com.yhj.wchat.controller;

import com.wf.captcha.ArithmeticCaptcha;
import com.yhj.wchat.entity.dto.LoginRequest;
import com.yhj.wchat.entity.dto.RegisterRequest;
import com.yhj.wchat.entity.dto.TokenUserInfoDto;
import com.yhj.wchat.entity.dto.UserRequest;
import com.yhj.wchat.entity.vo.CheckCodeVo;
import com.yhj.wchat.entity.vo.ResponseVO;
import com.yhj.wchat.entity.vo.UserInfoVO;
import com.yhj.wchat.exception.BusinessException;
import com.yhj.wchat.redis.RedisComponent;
import com.yhj.wchat.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

@RestController
@RequestMapping("/account")
@Slf4j

public class AccountController extends ABaseController{
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private RedisComponent redisComponent;

    @RequestMapping("/checkCode")
    public ResponseVO checkCode() {
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(100,42);
        String code = captcha.text();
        String checkCodeKey = redisComponent.saveCheckCode(code);
        String checkCodeBase64 = captcha.toBase64();
        CheckCodeVo checkCodeVo = new CheckCodeVo();
        checkCodeVo.setCheckCodeKey(checkCodeKey);
        checkCodeVo.setCheckCode(checkCodeBase64);
        return  getSuccessResponseVO(checkCodeVo);
    }

    @RequestMapping("/register")
    public ResponseVO register(@Valid @RequestBody RegisterRequest registerRequest, BindingResult result) {

        try {
            if (result.hasErrors()) {
                throw  new BusinessException("参数校验失败");
            }
            if (!registerRequest.getCheckCode().equalsIgnoreCase(redisComponent.getCheckCode(registerRequest.getCheckCodeKey()))){
                throw  new BusinessException("图片验证码不正确");
            }
            this.userInfoService.register(registerRequest.getEmail(),registerRequest.getNickName(),registerRequest.getPassword());
            return getSuccessResponseVO(null);
        } finally {
            log.info("清理验证码");
            redisComponent.cleanCheckCode(registerRequest.getCheckCodeKey());
        }
    }
    @RequestMapping("/login")
    public ResponseVO login(@Valid @RequestBody LoginRequest loginRequest, BindingResult result) {
        try {
            if( result.hasErrors()){
                throw new BusinessException("参数校验失败");
            }
            if (!loginRequest.getCheckCode().equalsIgnoreCase(redisComponent.getCheckCode(loginRequest.getCheckCodeKey()))){
                throw  new BusinessException("图片验证码不正确");
            }
            UserInfoVO userInfoVO = this.userInfoService.login(loginRequest.getEmail(),loginRequest.getPassword());
            return getSuccessResponseVO(userInfoVO);
        }finally {
            redisComponent.cleanCheckCode(loginRequest.getCheckCodeKey());
        }
    }
    @RequestMapping("/logout")
    public ResponseVO logout(@Valid @RequestBody UserRequest userRequest, BindingResult result) {

            if( result.hasErrors()){
                throw new BusinessException("参数校验失败");
            }

        String token = userRequest.getToken();
        TokenUserInfoDto tokenUserInfoDto = redisComponent.getTokenUserInfoDto(token);
        String userId = tokenUserInfoDto.getUserId();
        Boolean flag = userInfoService.logout(userId);

        return getSuccessResponseVO(flag);

    }
}
